<template>
  <div>
    <eForm ref="eform"/>
    <el-form ref="form" :model="form" :rules="rules" style="margin-top: 6px;" size="small" label-width="90px">
      <el-form-item label="输入Json" prop="body">
        <el-input v-model="form.body" style="weight:75%;" rows="20" type="textarea" @blur="doCheck"/>
      </el-form-item>
      <el-form-item label="生成类别" prop="generatorType">
        <el-radio-group v-model="form.generatorType" size="medium">
          <el-radio v-for="(item, index) in generatorTypeOptions" :key="index" :label="item.value"
                    :disabled="item.disabled">{{item.label}}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="">
        <el-button :loading="doSubmitLoading" size="medium" type="primary" @click="doSubmit">生成</el-button>
        <el-button :loading="doFormatLoading" size="medium" type="primary" @click="doFormat">格式化</el-button>
        <el-button :loading="configMessageLoading" size="medium" type="primary" @click="setConfigMessage">配置信息
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
    import {jsonCreateJavaEntity} from '@/api/code-tools'
    import {downloadFileFunction} from "@/api/downloadFile"
    import eForm from "./jsonToMysqlConfigMessageForm"


    export default {

        components: {eForm},
        data() {
            return {
                form: {
                    body: '',
                    generatorType: "mysql"

                },
                formatJson: '',
                doSubmitLoading: false,
                doFormatLoading: false,
                configMessageLoading: false,
                generatorTypeOptions: [{
                    "label": "Mysql版",
                    "value": "mysql"
                }, {
                    "label": "Entity版",
                    "value": "entity"
                }
                ],
                rules: {
                    body: [
                        {required: true, message: 'Json字符串不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            doSubmit() {
                const json = this.form.body;
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.showCheckResult(json)
                        if (json === null || json === undefined) {
                            this.$message({
                                message: 'json为空',
                                type: 'error'
                            })
                        } else {
                            const modelType = this.form.generatorType;
                            if (modelType == "mysql") {

                            } else if (modelType == "entity") {
                                jsonCreateJavaEntity(
                                    this.form.body
                                ).then(res => {
                                    this.loading = true
                                    downloadFileFunction(res, `entity.zip`)
                                    this.loading = false
                                }).catch(err => {
                                    this.loading = false
                                    console.log(err)
                                })
                            } else {
                            }
                        }
                    } else {
                        return false
                    }
                })


            },
            doFormat() {
                this.doCheck();
            },
            doCheck() {
                const json = this.form.body;
                if (!json) {
                    return
                }
                this.showCheckResult(json)
            },
            showCheckResult(json) {
                try {
                    const jsonString = JSON.parse(json)
                    this.form.body = JSON.stringify(jsonString, null, 4);
                } catch (e) {
                    this.$message({
                        message: 'json格式不正确',
                        type: 'error'
                    })
                }
            },
            setConfigMessage() {
                this.$refs.eform.dialog = true
                const modelType = this.form.generatorType;
                this.$refs.eform.doQueryGeneratorCodeConfig({configType: modelType});
            }
        }
    }
</script>

<style scoped>
</style>
